﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AssociationCorpLogic;

namespace AssociationCorpGUI
{
    public partial class frmAssociation : Form
    {
        public frmAssociation()
        {
            InitializeComponent();
        }

        private void AddNewAssociationToBoard(Association association)
        {
            this.SuspendLayout();
            guiAssociation newAssociation = new guiAssociation(association);
            //newAssociation.Location = new System.Drawing.Point(147, 150);
            newAssociation.Location = FindEmptySpot(new Point(50, 50)); //new System.Drawing.Point(147 + left, 150);
            newAssociation.Size = new System.Drawing.Size(association.Word.Length * 10, 40);
            newAssociation.Visible = true;
            //add listener for if the whole word is unlocked
            AddWholeWordUnlockedListener(newAssociation);
            this.pnlGamePanel.Controls.Add(newAssociation);
            this.pnlGamePanel.Refresh();
            this.ResumeLayout(true);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<Association> lst = new List<Association>();
            lst.Add(new Association("test1"));
            lst.Add(new Association("test2"));
            lst.Add(new Association("test3"));
            lst.Add(new Association("test4"));
            lst.Add(new Association("test5"));
            AddNewAssociationToBoard(new Association("testord", lst));
        }

        private void AddWholeWordUnlockedListener(guiAssociation association)
        {
            association.WholeWordUnlocked += new WholeWordUnlockedEventHandler(AddNewAssociations);
        }
        private void AddNewAssociations(object sender, WholeWordUnlockedEvent e)
        {
            if (e.Associations.Count == 0) { return; }
            this.SuspendLayout();
            Point previousLocation;
            //if (this.pnlGamePanel.Controls.Count > 0) { previousLocation = this.pnlGamePanel.Controls[this.pnlGamePanel.Controls.Count - 1].Location; }
            //else { previousLocation = new Point(50, 50); }
            foreach (Association a in e.Associations)
            {
                //TODO: 
                //här är jag nu: det som behövs göras är att placera ut de nya associationerna på lämpliga platser,
                //så att de inte krockar med redan utplacerade associationer
                //samt att det ska bildas en associationslinje mellan dem
                //skriv en funktion som kollar om hörnen (övre vänster/höger, nedre vänster/höger) ligger innanför
                //gränserna till någon annan, då skjuter den något på positionen och testar igen... typ.
                guiAssociation newAssociation = new guiAssociation(a);
                //left += 100;
                newAssociation.Size = new System.Drawing.Size(a.Word.Length * 10, 40);
                newAssociation.Location = FindEmptySpot(newAssociation); //new System.Drawing.Point(147 + left, 150);
                previousLocation = newAssociation.Location;
                newAssociation.Visible = true;
                this.pnlGamePanel.Controls.Add(newAssociation);
                this.pnlGamePanel.Refresh();
            }
            this.ResumeLayout(true);
        }

        private System.Drawing.Point FindEmptySpot(Point suggestedLocation, Size size)
        {
            Point leftTop = suggestedLocation;
            Point rightTop = new Point(suggestedLocation.X + size.Width, suggestedLocation.Y);
            Point leftBottom = new Point(suggestedLocation.X, suggestedLocation.Y + size.Height);
            Point rightBottom = new Point(suggestedLocation.X + size.Width, suggestedLocation.Y + size.Height);

            
            foreach (guiAssociation g in this.pnlGamePanel.Controls.OfType<guiAssociation>())
            {
                if (!(g.Bounds.Contains(leftTop) || g.Bounds.Contains(rightTop) || g.Bounds.Contains(leftBottom) || g.Bounds.Contains(rightBottom))) {
                    return leftTop;
                }
            }
            return null;
        }
    }
}
